import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        count: 100,
        msg: 'hello',
        sex: 1,
        user: {
            name: 'zhangsan',
            age: 19
        },
        userList: [
            { id: 1, name: 'zhangsan', sex: 1 },
            { id: 2, name: 'lisi', sex: 0 },
            { id: 3, name: 'luck', sex: 0 }
        ]
    },
    //格式化属性的
    getters: {
        formatterSex(state) {
            if (state.sex === 0) {
                return '女';
            } else {
                return '男';
            }
        },
        formatterUserList(state) {
            let userList = state.userList;
            userList.forEach(e => {
                if (e.sex === 1) {
                    e.sex = '男'
                } else if (e.sex === 0) {
                    e.sex = '女'
                } else {
                    e.sex = '未知'
                }
            })
            return userList;
        },
        getByName: (state) => (name) => {
            let userList = state.userList;
            return userList.filter(e => {
                return e.name === name;
            })
        },
        getNanList(state) {
            let userList = state.userList;
            return userList.filter(e => {
                return e.sex === '男';
            })
        },
        getNanSize(state, getters) {
            return getters.getNanList.length;
        }
    },
    mutations: {
        add123(state) {

            console.log("--------add123-------");
            state.count++;
        },
        addValue(state, n) {
            state.count = state.count + n;
        }
    },
    //如果说有异步处理或者请求接口 我都是通过actions ---->mutations
    actions: {
        add(context) {
            context.commit('add123')
        }
    },
    modules: {}
})